import { NavBar, DatePicker } from 'antd-mobile'
import './index.scss'
import { useEffect, useMemo, useState } from 'react'
import classNames from 'classnames'
import dayjs from 'dayjs'
import { useSelector } from 'react-redux'
import { groupBy } from 'lodash'
import DayBill from './components/Daybill'
const Month = () => {
  // 按月做数据的分组
  const { billList } = useSelector(state => state.bill)
  const billGroup = useMemo(() => {
    return groupBy(billList, (item) => dayjs(item.date).format('YYYY-MM'))
  }, [billList])
  // 控制弹框的打开和关闭
  const [dataVisible, setDataVisible] = useState(false)
  // 控制时间显示
  const [currentDate, setCurrentDate] = useState(() => {
    return dayjs(new Date()).format('YYYY-MM')
  })

  // 初始化的时候把当前月的统计数据显示出来
  useEffect(() => {
    if (billGroup) {
      setMonthList(billGroup[currentDate])
    }
    // onConfirm(currentDate)
  }, [billGroup])
  const [currentMothList, setMonthList] = useState([])
  const monthResult = useMemo(() => {
    //支出  收入  结余
    let pay = 0
    let income = 0
    if (currentMothList) {
      pay = currentMothList.filter(item => item.type === 'pay').reduce((pre, now) => pre + now.money, 0)
      income = currentMothList.filter(item => item.type === 'income').reduce((pre, now) => pre + now.money, 0)
    }

    return {
      pay,
      income,
      total: pay + income
    }
  }, [currentMothList])

  // 确认回调
  const onConfirm = (date) => {
    setDataVisible(false)
    // 其他逻辑
    const formatDate = dayjs(date).format('YYYY-MM')
    setCurrentDate(formatDate);
    setMonthList(billGroup[formatDate])
  }

  // 当前月按照日来做分组
  const dayGroup = useMemo(() => {
    const groupDate = groupBy(currentMothList, (item) => dayjs(item.date).format('YYYY-MM-DD'))
    const keys = Object.keys(groupDate)
    return {
      groupDate,
      keys
    }
  }, [currentMothList])

  return (
    <div className="monthlyBill">
      <NavBar className="nav" backArrow={false}>
        月度收支
      </NavBar>
      <div className="content">
        <div className="header">
          {/* 时间切换区域 */}
          <div className="date" onClick={() => setDataVisible(true)}>
            <span className="text">
              {currentDate}月账单
            </span>
            {/* 思路：根据当前弹框打开的状态控制expand类名是否存在 */}
            <span className={classNames('arrow', dataVisible && 'expand')}></span>
          </div>
          {/* 统计区域 */}
          <div className='twoLineOverview'>
            <div className="item">
              <span className="money">{monthResult.pay.toFixed(2)}</span>
              <span className="type">支出</span>
            </div>
            <div className="item">
              <span className="money">{monthResult.income.toFixed(2)}</span>
              <span className="type">收入</span>
            </div>
            <div className="item">
              <span className="money">{monthResult.total.toFixed(2)}</span>
              <span className="type">结余</span>
            </div>
          </div>
          {/* 时间选择器 */}
          <DatePicker
            className="kaDate"
            title="记账日期"
            precision="month"
            visible={dataVisible}
            // onCancel={() => setDataVisible(false)}
            onConfirm={onConfirm}
            onClose={() => setDataVisible(false)}
            max={new Date()}
          />
        </div>
        {/* 单日账单统计 */}
        {
          dayGroup.keys.map(key => {
            return <DayBill date={key} billList={dayGroup.groupDate[key]} />
          })
        }
      </div>
    </div >
  )
}

export default Month